home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / aa_m68k_Intel_Only / ToyViewer1.2 / Source / mag2pxo.tproj / magheader.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-21  |  2.8 KB  |  135 lines

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <stdio.h>
  4. #include <libc.h>
  5.  
  6. #include "mag.h"
  7.  
  8. int get_short(FILE *fp)
  9. {
  10.     int c = getc(fp);
  11.     return ((getc(fp) << 8) | c);
  12. }
  13.  
  14. long get_long(FILE *fp)
  15. {
  16.     long c = get_short(fp);
  17.     return ((get_short(fp) << 16) | c);
  18. }
  19.  
  20.  
  21. extern void sjis2euc(unsigned char *, unsigned char *);
  22.  
  23. static void copy_comm(char *comm, char *memo)
  24. {
  25.     int i, k, n;
  26.     char *p;
  27.  
  28.     if (memo == NULL || *memo == 0) return;
  29.     n = strlen(memo);
  30.     p = (char *)malloc(n + 2);
  31.     sjis2euc(p, memo);
  32.     p[n+1] = 0;    /* sentinel */
  33.     for (i = 0, k = 0; i < MAX_COMMENT-2 && p[k]; ) {
  34.         if (p[k] == ' ') {
  35.             for (n = 0; p[++k] == ' '; n++) ;
  36.             comm[i++] = ' ';
  37.             if (n) comm[i++] = ' ';
  38.         }else if (p[k] & 0x80) {
  39.             comm[i++] = p[k++];
  40.             comm[i++] = p[k++];
  41.         }else
  42.             comm[i++] = p[k++];
  43.     }
  44.     comm[i] = 0;
  45.     free((void *)p);
  46. }
  47.  
  48. static magHeader *get_header(FILE *fp) /* ¥à¥¡¥⁄¥º⁄«⁄إ㥈¥¹¬ö˚ú⁄ù˘²⁄ì¡£ */
  49. {
  50.     int cc, x1, x2, y1, y2;
  51.     magHeader *mh;
  52.  
  53.     mh = (magHeader *)malloc(sizeof(magHeader));
  54.     (void)fseek(fp, 3L, SEEK_CUR);
  55.     cc = getc(fp);
  56.     mh->is256c = (cc & 0x80) ? YES : NO;
  57.     mh->isDouble = (cc & 0x01) ? YES : NO;
  58.     x1 = get_short(fp);
  59.     y1 = get_short(fp);
  60.     x2 = get_short(fp);
  61.     y2 = get_short(fp);
  62.     if (x1 < 0 || x2 >= MaxImageSize || y1 < 0 || y2 >= MaxImageSize)
  63.         mh->xbitwidth = mh->yheight = mh->xbytewidth = 0;
  64.     else {
  65.         mh->xbitwidth = x2 - x1 + 1;
  66.         mh->yheight = y2 - y1 + 1;
  67.         mh->xbytewidth = (x2 >> 3) - (x1 >> 3) + 1;
  68.     }
  69.     mh->flagAoffset = get_long(fp);
  70.     mh->flagBoffset = get_long(fp);
  71.     mh->flagBsize = get_long(fp);
  72.     mh->pixeloffset = get_long(fp);
  73.     mh->pixelsize = get_long(fp);
  74.     return mh;
  75. }
  76.  
  77. void freeMagHeader(magHeader *mh)
  78. {
  79.     if (mh) free((void *)mh);
  80. }
  81.  
  82. magHeader *loadMagHeader(FILE *fp, long *base, int *errcode)
  83.      /* ¥à¥¡¥⁄¥º⁄«⁄إ㥈¥¹¬ö˚ú⁄ù˘²⁄ì¡£
  84.     ¥¤¥Ø¡…⁄‹¦fl⁄›⁄¿¬ò„ñ⁄ˇ NULL⁄‹˚á⁄Œ¡¢errcode⁄¸⁄‰⁄˛˝ÿ˝‡⁄‹˘⁄º¡£
  85.     ¥à¥¡¥⁄¥º¥é¥⁄¥ú¥¿⁄ˇ¥±¥ò¥ˆ¥¨¬ö˚ú⁄˛¹Ł˘‹⁄ù»ã⁄•⁄˘˚á⁄º¡£ */
  86. {
  87.     char    typestr[10];
  88.     long    size, w;
  89.     int    i, cc;
  90.     unsigned char    *mm;
  91.     struct stat    sbuf;
  92.     magHeader    *mh;
  93.  
  94.     *errcode = 0;
  95.     fstat(fileno(fp), &sbuf);
  96.     size = sbuf.st_size;
  97.     for (i=0; i<8; i++)
  98.         typestr[i] = getc(fp);
  99.     typestr[8] = 0;
  100.     if (strcmp(typestr, "MAKI02  ") != 0) {
  101.         *errcode = Err_FORMAT;
  102.         return NULL;
  103.     }
  104.     for (i=8; (cc=getc(fp)) != 0x1a; i++)
  105.         if (cc == EOF) {
  106.             *errcode = Err_SHORT;
  107.             return NULL;
  108.         }
  109.     mm = (unsigned char *)malloc(i-6);
  110.     (void)fseek(fp, 8L, SEEK_SET);
  111.     for (i=0; (cc=getc(fp)) != 0x1a; i++)
  112.         mm[i] = cc;
  113.     mm[i++] = 0;
  114.     if ((*base = i + 8) + sizeof_magHeader >= size) {
  115.         *errcode = Err_SHORT;
  116.         return NULL;
  117.     }
  118.     mh = get_header(fp);
  119.     copy_comm(mh->memo, mm);
  120.     free((void *)mm);
  121.     if (mh->xbitwidth <= 0) {
  122.         freeMagHeader(mh);
  123.         *errcode = Err_ILLG;
  124.         return NULL;
  125.     }
  126.     if ((w = *base + mh->pixeloffset + mh->pixelsize) > size)
  127.     /* ¥˙¡…¥¿•ñ´»¡£´¿¬fl⁄ˇ‚«˘¤⁄„¡£ */
  128.         if (w - size > 8) {
  129.             freeMagHeader(mh);
  130.             *errcode = Err_SHORT;
  131.             return NULL;
  132.         }
  133.     return mh;
  134. }
  135.